import 'package:eh_mobile/common/routers/index.dart';
import 'package:eh_mobile/common/store/user_store.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../store/config_store.dart';

/// 检查是否登录
class RouteAuthMiddleware extends GetMiddleware {
  // priority 数字小优先级高
  @override
  RouteAuthMiddleware({required this.priority});

  @override
  // ignore: overridden_fields
  int? priority = 0;

  @override
  RouteSettings? redirect(String? route) {
    if (UserStore().isLogin || route == RouteNames.LOGIN) {
      return null;
    } else {
      if (!ConfigStore().isFirstOpen) {
        Future.delayed(
            const Duration(seconds: 1), () => Get.snackbar("提示", "登录过期,请重新登录"));
      }
      return const RouteSettings(name: RouteNames.LOGIN);
    }
  }
}
